package com.dgut.demo.service.impl;

import com.dgut.demo.bean.GoodsMessage;
import com.dgut.demo.bean.SupplierMessage;
import com.dgut.demo.bean.User;
import com.dgut.demo.bean.VipMessage;
import com.dgut.demo.controller.LogsController;
import com.dgut.demo.mapper.SupplierMapper;
import com.dgut.demo.service.SupplierService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * @Auther 虎
 * @Data 2021/2/4 23:29
 * @Version 1.0
 */
@Service
public class SupplierServiceImpl implements SupplierService {

    @Autowired
    private SupplierMapper supplierMapper;
    @Autowired
    private LogsController logsController;
    @Autowired
    private User user;

    @Override
    public List<SupplierMessage> getAllSupplierMessage() {
        return supplierMapper.getAllSupplierMessage();
    }

    @Override
    public int addOneSupplierMessage(SupplierMessage supplierMessage) {
        Date date = new Date(System.currentTimeMillis());
        SimpleDateFormat dateFormat = new SimpleDateFormat("yyMMddHHmmss");
        int random = (int)(Math.random()*10);
        String sid='2'+ dateFormat.format(date) + random;
        supplierMessage.setSid(sid);
        int flag;
        flag = supplierMapper.addOneSupplierMessage(supplierMessage);
        if(flag == 1){
            //log
            String actions = "新增供应商【"+supplierMapper.getSupplierMessageById(sid).toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
            return 1;
        }
        return 0;
    }

    @Override
    public int deleteOneSupplierMessage(String sid) {
        SupplierMessage supplierMessage = supplierMapper.getSupplierMessageById(sid).get(0);
        //log
        String actions = "删除供应商【"+supplierMessage.toString()+"】";
        String userId = user.getUserId();
        logsController.addOneLogsMessage(actions,userId);

        return supplierMapper.deleteOneSupplierMessage(sid);
    }

    @Override
    public int updateOneSupplierMessage(SupplierMessage supplierMessage) {
        SupplierMessage supplierMessage1 = supplierMapper.getSupplierMessageById(supplierMessage.getSid()).get(0);
        int flag = supplierMapper.updateOneSupplierMessage(supplierMessage);
        if(flag == 1){
            SupplierMessage supplierMessage2 = supplierMapper.getSupplierMessageById(supplierMessage.getSid()).get(0);
            //log
            String actions = "更新供应商信息【"+supplierMessage1.toString()+"】--》【"+supplierMessage2.toString()+"】";
            String userId = user.getUserId();
            logsController.addOneLogsMessage(actions,userId);
        }
        return flag;
    }

    @Override
    public List<SupplierMessage> getSupplierMessageById(String sid) {
        return supplierMapper.getSupplierMessageById(sid);
    }

    @Override
    public List<SupplierMessage> getSupplierMessageByName(String sname) {
        return supplierMapper.getSupplierMessageByName(sname);
    }

    @Override
    public List<SupplierMessage> getSupplierMessageByPhone(String sphone) {
        return supplierMapper.getSupplierMessageByPhone(sphone);
    }

    @Override
    public List<SupplierMessage> getSupplierMessageByMaterial(String smaterial) {
        return supplierMapper.getSupplierMessageByMaterial(smaterial);
    }

    @Override
    public List<SupplierMessage> getSupplierMessageByAddress(String saddress) {
        return supplierMapper.getSupplierMessageByAddress(saddress);
    }
}
